package com.lj.universalframework.app

import android.app.Application
import android.content.Context
import android.content.IntentFilter
import android.net.ConnectivityManager
import com.lj.universalframework.R
import com.lj.universalframework.http.Request
import com.lj.universalframework.receiver.NetworkChangeReceiver
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.FormatStrategy
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import kotlin.properties.Delegates


/**
 * <p>文件名：MyApplication
 * <p>创建者：LJ
 * <p>创建日期：2020\7\14 0014 14:01
 * <p>描述：
 */
class MyApplication : Application() {
    companion object {
        var mContext: Context by Delegates.notNull()
        var mInstance: MyApplication by Delegates.notNull()
    }

    override fun onCreate() {
        super.onCreate()
        mContext = applicationContext
        mInstance = this

        val filter = IntentFilter()
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
        registerReceiver(NetworkChangeReceiver(), filter)

        /**
         * 初始化网络请求
         * 配置公共网络请求头
         */
        Request.init(this, "https://gank.io/api/v2/data/category/") {
            okHttp {
                //配置okhttp
                it
            }
            retrofit {
                //配置retrofit
                it
            }
        }

        val formatStrategy: FormatStrategy = PrettyFormatStrategy.newBuilder()
            .showThreadInfo(true) // (Optional) 是否显示线程信息。默认为true
            .methodCount(0) // (Optional) 要显示多少个方法行。默认值2
            .methodOffset(7) // (Optional) 隐藏内部方法调用直到offset。默认5
            .tag("测试一下")
            .build()
        Logger.addLogAdapter(AndroidLogAdapter(formatStrategy))

        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            layout.setPrimaryColorsId(R.color.colorAccent, R.color.white) //全局设置主题颜色
            ClassicsHeader(context)
        }
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ ->
            //指定为经典Footer，默认是 BallPulseFooter
            ClassicsFooter(context).setDrawableSize(20f)
        }
    }
}